package com.it.you.base.stream;

import cn.hutool.core.date.DateUtil;
import com.it.you.pojo.People;
import com.it.you.pojo.Student;
import lombok.SneakyThrows;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class StreamTest3 {

    @SneakyThrows
    public static void main(String[] args) {

        List<Student> students = new ArrayList<>();
        Student s1 = new Student("you", 18, new Date(DateUtil.parseDate("1992-06-19").getTime()), "运城", new BigDecimal("50000000"));
        Student s2 = new Student("you", 16, new Date(DateUtil.parseDate("1994-03-14").getTime()), "运城", new BigDecimal("20000000"));
        Student s3 = new Student("you", 18, new Date(DateUtil.parseDate("1965-10-14").getTime()), "黄陂", new BigDecimal("20000000"));
        Student s4 = new Student("you", 18, new Date(DateUtil.parseDate("1964-08-03").getTime()), "武汉", new BigDecimal("40000000"));

        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);

        //按照多个字段去重
        ArrayList<Student> collect = students.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new));
        System.out.println(collect.size());

        long count = students.stream().map(vo -> vo.getName() + vo.getAge()).distinct().count();
        System.out.println(count);

        String collect1 = students.stream().filter(s -> s.getAge() == 0).map(Student::getName).distinct().collect(Collectors.joining(","));
        System.out.println(collect1);

        String res = students.stream().limit(2).map(Student::getName).collect(Collectors.joining(",")) + "...";
        System.out.println(res);

        Map<People, List<Student>> pe = students.stream().collect(Collectors.groupingBy(s -> {
            People people = new People();
            people.setName(s.getName());
            people.setAddress(s.getAddress());
            people.setAge(s.getAge());
            people.setBirthDate(s.getBirthDate());
            return people;
        }));
        System.out.println(pe);
        System.out.println(pe.size());

        Map<String, List<Student>> pe1 = students.stream().collect(Collectors.groupingBy(Student::getName));
        System.out.println(pe1);
        System.out.println(pe1.size());
    }
}
